package com.meixiang.fragment.account;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.account.manager.BindPhoneActivity;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseFragment;
import com.meixiang.util.DesUtil;
import com.meixiang.view.PaymentKeyDialog;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 绑定手机——身份验证
 *
 * @author LZRUI
 */
public class VerifyIdentityFragment extends BaseFragment implements PaymentKeyDialog.InputFinishListener {

    @Bind(R.id.layout_password)
    PaymentKeyDialog layoutPassword;

    private IBindPhoneClickListener clickListener;

    public VerifyIdentityFragment() {

    }

    public static VerifyIdentityFragment newInstance() {
        return new VerifyIdentityFragment();
    }

    @Override
    public void onAttach(Context activity) {
        super.onAttach(activity);
        if (activity instanceof IBindPhoneClickListener) {
            clickListener = (IBindPhoneClickListener) activity;
        } else {
            throw new RuntimeException(activity.toString()
                    + " must implement IVerifyIdentityFragmentToNext");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_verify_identity, container, false);
        ButterKnife.bind(this, rootView);
        super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        layoutPassword.setKeyTitle(R.string.verify_pay_off_password);
        layoutPassword.setInputFinishListener(this);
    }


    @Override
    protected void setData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void verifyPayment(String password) {
        HttpUtils.post(Config.VERIFY_PAYMENT, BindPhoneActivity.TAG, new HttpParams("password", DesUtil.md5(password)), new HttpCallBack(activity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if (clickListener != null) {
                    clickListener.onVerifyIdentityFragmentClick();
                }
            }

            @Override
            public void onError(String code, String message) {
                if (isVisible()) {
                    Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onInputFinish(String pwd) {
        verifyPayment(pwd);
    }
}
